package cn.tedu.vrd.controller;

import cn.tedu.vrd.entity.Product;
import cn.tedu.vrd.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class ProductController {

    @Autowired(required = false)
    ProductMapper mapper;

    //    读取配置文件中的路径
    @Value("${dirPath}")
    private String dirPath;

    @RequestMapping("/product/insert")
    public void insert(Product product, MultipartFile file) throws IOException {
        System.out.println("product = " + product + ", file = " + file);
        String fileName=file.getOriginalFilename();
        String suffix=fileName.substring(fileName.lastIndexOf("."));
        /*得到一个日期路径yyyy年MM月dd日      HH小时 mm分钟  ss秒*/
        fileName= UUID.randomUUID()+suffix;
        SimpleDateFormat format=
                new SimpleDateFormat("/yyyy/MM/dd/");
        Date date=new Date();
        String datePath= format.format(date);
        File dirFile=new File(dirPath+datePath);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
//        文件的完整路径
        String filePath=dirPath+datePath+fileName;
        file.transferTo(new File(filePath));
//          把图片路径给到作品对象
        product.setUrl(datePath+fileName);
//        保存到数据库中
        mapper.insert(product);
    }

    @RequestMapping("/product/select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("/product/delete")
    public void delete(int id){
//        通过id查询作品信息
        Product product=mapper.selectById(id);
//        拼接出图片文件的完整路径
        String filePath =dirPath+"/"+product.getUrl();
//        删除文件
        new File(filePath).delete();

        mapper.delete(id);
    }

    @RequestMapping("/product/selectbycid")
    public List<Product> selectByCid(int cid){
        System.out.println("cid = " + cid);
        return mapper.selectByCid(cid);
    }

    @RequestMapping("/product/selectbywd")
    public List<Product> selectByWd(String wd){
        System.out.println("wd = " + wd);
        return mapper.selectByWd(wd);
    }

    @RequestMapping("/product/selectbyview")
    public List<Product> selectByView(){
        return mapper.selectByView();
    }

    @RequestMapping("/product/selectbylike")
    public List<Product> selectByLike(){
        return mapper.selectByLike();
    }

    @RequestMapping("/product/selectbyid")
    public Product selectById(int id,HttpSession session){
        String info=(String) session.getAttribute("view"+id);
        if (info==null){
            //        让浏览量+1
            mapper.view(id);
//            把id保存到Session对象中
            session.setAttribute("view"+id,"浏览过");
        }


        System.out.println("id = " + id);
        return mapper.selectById(id);
    }

    @RequestMapping("/product/like")
    public int like(int id, HttpSession session){
        //从Session会话对象中取出曾经保存的信息 看是否有值
        String info = (String) session.getAttribute("like"+id);
        //如果没有得到内容 说明没有点过赞
        if (info==null){
            //处理点赞
            mapper.like(id);
            //把点赞的信息保存到Session中
            session.setAttribute("like"+id,"点过赞!");
            return 1;//点赞成功!
        }
        return 0;//代表已经点过赞
    }

}
